

package org.apache.ocean.util;

import java.util.concurrent.*;
import java.util.concurrent.locks.*;

/**
 *
 * @author jasonr
 */
public class SimpleLatch {
  private LinkedBlockingQueue queue = new LinkedBlockingQueue(2);
  private ReentrantLock lock = new ReentrantLock();
  
  public SimpleLatch() {
  }
  
  public void signal() throws InterruptedException {
    lock.lock();
    try {
      if (queue.size() == 0) queue.put(new Object());
    } finally {
      lock.unlock();
    }
  }
  
  public void await(long timeout) throws InterruptedException {
    queue.poll(timeout, TimeUnit.MILLISECONDS);
  }
  
  public void await() throws InterruptedException {
    queue.take();
  }
}
